<html>
<head><meta charset="utf-8"><title>Does the compiler do these optimizations? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html">Does the compiler do these optimizations?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221682905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221682905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221682905">(Jan 05 2021 at 18:27)</a>:</h4>
<p>I was wondering about two things regarding the compiler:</p>
<ol>
<li>For output to the terminal, does the compiler buffer all the output into a single string and then print it using a single print operation at the end?</li>
<li>For fast compile times, does the compiler check the file metadata to check stats like the last edit time? Then it doesn't have to recompile that part<br>
These are no-brainers but I was curious</li>
</ol>



<a name="221684714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221684714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221684714">(Jan 05 2021 at 18:41)</a>:</h4>
<ol>
<li>This is a library thing, not compiler thing. The standard library line buffers stdout. It doesn't buffer stderr.</li>
<li>For whole crates, cargo checks the mtime of all files used for compiling the crate and avoids recompiling if those and all dependencies of the crate are not modified. In debug mode, cargo enabled the incremental mode of rustc, which causes it to keep a cache containing the result of many so called queries together with dependency information. The query results are reused when possible.</li>
</ol>



<a name="221684726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221684726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221684726">(Jan 05 2021 at 18:41)</a>:</h4>
<p><span class="user-mention" data-user-id="376093">@ruster</span> ^</p>



<a name="221685425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221685425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221685425">(Jan 05 2021 at 18:47)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span>  1. Thank you. Something just clicked in my head. So that is why nothing is printed with just a <code>print!()</code> until you flush the output. However, for <code>println!()</code> it does immediately print to the terminal, so for that doing the <code>let mut buffer = String::with_capacity();</code> <code>buffer.push_str();</code>-thing should be faster, right? But I suppose the compiler only uses the <code>print!()</code>?</p>
<ol start="2">
<li>As expected. Thank you for this interesting information</li>
</ol>



<a name="221685989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221685989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221685989">(Jan 05 2021 at 18:52)</a>:</h4>
<p><code>println!()</code> prints immediately to the terminal as it ends with a trailing <code>\n</code>. It is literally implemented as <code>print!()</code> with a <code>\n</code> appended to the format string. Printing immediately by the way means that it buffers until the end of the line and then uses a single <code>write</code> syscall to write the whole line. If you were to print two lines using <code>println!()</code>, you would get two <code>write</code> syscalls I think.</p>



<a name="221686399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221686399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221686399">(Jan 05 2021 at 18:54)</a>:</h4>
<p>Right, that's what I thought too :)</p>



<a name="221692766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221692766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221692766">(Jan 05 2021 at 19:49)</a>:</h4>
<p>However, if I run the following code, the string buffer version is consistently faster...<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=44f620d33e971e576c80ec80f5fad02a">https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2018&amp;gist=44f620d33e971e576c80ec80f5fad02a</a><br>
Can anyone explain this to me?</p>



<a name="221694688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221694688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221694688">(Jan 05 2021 at 20:04)</a>:</h4>
<p>I suspect a lot of that is locking overhead.</p>



<a name="221695014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Does%20the%20compiler%20do%20these%20optimizations%3F/near/221695014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Does.20the.20compiler.20do.20these.20optimizations.3F.html#221695014">(Jan 05 2021 at 20:07)</a>:</h4>
<p>but I wouldn't get too hung up on this from the compiler perspective -- printing to stdout is not where we spend much time</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>